package com.hzpd.hainan.module.newspager;

import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hzpd.hainan.R;
import com.hzpd.hainan.api.InterfaceJsonfile;
import com.hzpd.hainan.app.BaseFragment;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.model.event.ShowOrHideBackEvent;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/7/12.
 */

public class ServiceFragemnt_new extends BaseFragment {
    @BindView(R.id.szb_webview)
    WebView szb_webview;
    private ShowOrHideBackEvent event;
    @Override
    public int setMyContentView() {
        return R.layout.fragment_service_layout;
    }
    public static ServiceFragemnt_new newInstance() {
        ServiceFragemnt_new newsPagerFragment = new ServiceFragemnt_new();
        return newsPagerFragment;
    }
    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        event = new ShowOrHideBackEvent();
        WebSettings webSettings = szb_webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);//设置缩放
        webSettings.setBuiltInZoomControls(true);//设置出现缩放工具

        webSettings.setUseWideViewPort(true);//扩大比例的缩放
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);
        szb_webview.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                if(webcanback()){
                    event.setShow(true);
                }else{
                    event.setShow(false);
                }
                EventBus.getDefault().post(event);
                super.onPageFinished(view, url);
            }
        });
        szb_webview.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && szb_webview.canGoBack()) {
                        //表示按返回键 时的操作
                        szb_webview.goBack(); //后退
                        return true; //已处理 ...
                    }

                }
                return false;
            }
        });
        if(spu.getLanguage().equals(CODE.ZHONGWEN)) {
            szb_webview.loadUrl(InterfaceJsonfile.NEW_SERVICE_WEBWIEW);
        }else{
            szb_webview.loadUrl(InterfaceJsonfile.NEW_SERVICE_WEBWIEW_ZANGWEN);
        }

    }
    public void doback(){
        if(szb_webview.canGoBack()){
            szb_webview.goBack();
        }
    }
    public boolean webcanback(){
        if(szb_webview != null) {
            return szb_webview.canGoBack();
        }else{
            return false;
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
